window.onload = function() {
  var vm = new Vue({
    el: "#community",
    data: {
      communityId: "",
      brandId: "",
      communitykeywords: "",
      communityBaseInfoList: {},
      attentionTypeNum: 0, //关注人数
      countVisitNum: 0, //浏览量次数
      communityHouse: {},
      communityList: [],
      moreBrowseNum: [],
      communityPicList: [], //服务设施图片
      isAttentionNum: 0, //判断页面底部收藏
      communityPhone: ""
    },
    created: function() {
      if (this.getParams("brochureId")) {
        this.getBrochureUserInfo();
      }
      this.communityId = this.getParams("id");
      this.getCommunityBaseInfo();
      this.getAttentionTypeNum();
      this.getCountVisit();
      this.getHouseList();
      this.getCommunityPic();
      this.attention();
    },
    methods: {
      //基本信息
      getCommunityBaseInfo() {
        var $this = this;
        house_server.getCommunityBaseInfo(
          { communityId: this.communityId },
          function(data) {
            if (!$this.communityPhone) {
              $this.communityPhone = data.data.phone;
            }
            $this.communityBaseInfoList = data.data;
            // console.log($this.communityBaseInfoList);
            $this.setMap(data.data);
          }
        );
      },
      //获取楼书用户信息
      getBrochureUserInfo: function() {
        var $this = this;
        house_server.brochureUserInfo(
          {
            brochureId: this.getParams("brochureId")
          },
          function(data) {
            $this.communityPhone = data.data.phone;
          }
        );
      },
      //关注人数
      getAttentionTypeNum() {
        var $this = this;
        house_server.getAttentionTypeNum(
          { id: this.communityId, type: 2 },
          function(data) {
            $this.attentionTypeNum = data.data;
          }
        );
      },
      //浏览量次数
      getCountVisit() {
        var $this = this;
        house_server.getCountVisit({ id: this.communityId, type: 2 }, function(
          data
        ) {
          $this.countVisitNum = data.data;
        });
      },
      //
      getHouseList() {
        var $this = this;
        house_server.getHouseList(
          { communityId: this.communityId, keyword: "" },
          function(data) {
            $this.communityHouse = data.data;
            $this.brandId = data.data.brandId;
            $this.getCommunityList(data.data.brandId);
          }
        );
      },
      //服务设施
      getCommunityPic() {
        var $this = this;
        house_server.getCommunityPic(
          { communityId: this.communityId },
          function(data) {
            $this.communityPicList = data.data;
          }
        );
      },
      //获取社区列表
      getCommunityList(brandId) {
        var $this = this;
        let idList = [];
        house_server.getCommunityList({ brandId: this.brandId }, function(
          data
        ) {
          $this.communityList = data.data.communityList;
          data.data.communityList.forEach(function(item) {
            idList.push(item.communityId);
          });
          $this.getCountVisits(idList);
        });
      },
      //获取更多社区浏览量量
      getCountVisits(list) {
        var $this = this;
        house_server.getCountVisits({ ids: list, type: 2 }, function(data) {
          $this.moreBrowseNum = data.data;
        });
      },
      goCommunity(id) {
        window.location.href = "./community.html?id=" + id + "";
      },
      //搜索
      goToEntry() {
        window.location.href = "./../search/entry.html?type=2";
      },
      //进入品牌
      goToBrand() {
        window.location.href = "./brand.html?id=" + this.brandId;
      },
      attention() {
        var $this = this;
        var token = "";
        if (this.getCookie("signature")) {
          token = this.getCookie("signature");
        }
        if (token) {
          other_server.attention(
            {
              type: "2",
              id: $this.communityId
            },
            function(data) {
              $this.isAttentionNum = data.data;
            }
          );
        }
      },
      //点击关注
      addAttention() {
        var $this = this;
        var token = "";
        if (this.getCookie("signature")) {
          token = this.getCookie("signature");
        }
        if (token) {
          other_server.addAttention(
            {
              attentionType: "2",
              id: $this.communityId
            },
            function(data) {
              $this.attention();
            }
          );
        } else {
          window.location.href = "/console/#/Login";
        }
      },
      goHome() {
        window.location.href = "/home/html/";
      },
      //地图
      setMap(data) {
        var longitude = data.longitude,
          latitude = data.latitude;
        var map = new BMap.Map("community-map");
        var point = new BMap.Point(longitude, latitude);
        map.centerAndZoom(point, 15);
        var marker = new BMap.Marker(point);
        map.addOverlay(marker);
      },
      // 获取cookie
      getCookie: function(cname) {
        var name = cname + "=";
        var ca = document.cookie.split(";");
        for (var i = 0; i < ca.length; i++) {
          var c = ca[i].trim();
          if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
        }
        return "";
      },
      //图片解析
      getImgUrl: function() {
        var ip = window.location.host,
          url = "";
        if (ip.indexOf("127.0.0.1") >= 0 || ip.indexOf("localhost") >= 0) {
          url = "http://114.113.68.14:9000/image/";
        } else if (ip.indexOf("www.") >= 0) {
          url = "http://image.3dzhixuan.com/image/";
        } else {
          url = "http://" + ip + "/image/";
        }
        return url;
      },
      // 获取参数
      getParams(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
      }
    }
  });
};
